package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.domain.dto.AfterSaleReplySaveDTO;
import cn.com.dcsgo.domain.po.AfterSale;
import cn.com.dcsgo.domain.vo.AfterSaleReplyDetailVO;
import cn.com.dcsgo.service.AfterSaleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dcsgo.domain.po.AfterSaleReply;
import cn.com.dcsgo.service.AfterSaleReplyService;
import cn.com.dcsgo.mapper.AfterSaleReplyMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 针对表【after_sale_reply(售后服务回复表)】的数据库操作Service实现
 *
 * @author Dcsgo
 * @since 2025-05-19 10:59:29
 */
@Service
@RequiredArgsConstructor
public class AfterSaleReplyServiceImpl extends ServiceImpl<AfterSaleReplyMapper, AfterSaleReply>
        implements AfterSaleReplyService {
    private final AfterSaleService afterSaleService;

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public void save(AfterSaleReplySaveDTO afterSaleReplySaveDTO) {
        // 检查当前售后服务是否已完成，已完成的售后服务不允许回复
        AfterSale afterSale = afterSaleService.getById(afterSaleReplySaveDTO.getAfterSaleId());
        // TODO 根据数据字典完成售后服务状态的判断及其处理流程
        if ("已完成".equals(afterSale.getState())) {
            throw new IllegalArgumentException("当前售后服务已完成，不能再进行回复");
        }
        // 封装售后服务回复记录并添加到数据库中
        AfterSaleReply afterSaleReply = new AfterSaleReply();
        afterSaleReply.setAfterSaleId(afterSaleReplySaveDTO.getAfterSaleId());
        afterSaleReply.setContent(afterSaleReplySaveDTO.getContent());
        // 回复状态当前先固定为 已发送
        afterSaleReply.setState("已发送");
        // 回复时间为当前系统时间
        afterSaleReply.setReplyTime(LocalDateTime.now());
        // TODO 后期添加登录后改进为: 默认使用当前登录用户id
        afterSaleReply.setEmpId(1L);

        this.save(afterSaleReply);
    }

    @Override
    public List<AfterSaleReplyDetailVO> list(Long afterSaleId) {
        if (afterSaleId == null) {
            return new ArrayList<>(0);
        }
        List<AfterSaleReply> afterSaleReplyList = this.list(
                new LambdaQueryWrapper<AfterSaleReply>()
                        .eq(AfterSaleReply::getAfterSaleId, afterSaleId)
                        .orderByDesc(AfterSaleReply::getReplyTime)
        );
        LinkedList<AfterSaleReplyDetailVO> afterSaleReplyDetailVOList = new LinkedList<>();
        afterSaleReplyList.forEach(afterSaleReply -> {
            AfterSaleReplyDetailVO afterSaleReplyDetailVO = new AfterSaleReplyDetailVO();
            afterSaleReplyDetailVO.setId(afterSaleReply.getId());
            afterSaleReplyDetailVO.setContent(afterSaleReply.getContent());
            afterSaleReplyDetailVO.setState(afterSaleReply.getState());
            afterSaleReplyDetailVO.setReplyTime(afterSaleReply.getReplyTime());
            afterSaleReplyDetailVO.setEmpId(afterSaleReply.getEmpId());
            afterSaleReplyDetailVOList.add(afterSaleReplyDetailVO);
        });
        return afterSaleReplyDetailVOList;
    }
}




